<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.wan888888.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Admin\Model\UserModel;

/**
 * 医生控制器
 * @author xu
 */
class MemberController extends AdminController {

    /**
     * 医生列表
     * @author xu
     */
    public function index(){
        $name       =   I('name');
        $phone      =   I('phone');
        $hospital   =   I('hospital');
        $map['is_show']  =   array('eq',1);
        $map['name']    =   array('like', '%'.(string)$name.'%');
        $map['phone']    =   array('like', '%'.(string)$phone.'%');
        $map['hospital']    =   array('like', '%'.(string)$hospital.'%');

        $total =UserModel::singleton()->getCount($map);
        $limit = $this->pagination($total);
        $list = UserModel::singleton()->getList($map,$limit);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->display();
    }

    /**
     * 医生列表
     * @author xu
     */
    public function update(){
        if(IS_GET){
            $id = I('get.id');
            empty($id) && $this->error('参数不能为空！');
            $data = UserModel::singleton()->getOne($id);
            $this->assign('data',$data);
            $this->display('edit');
        }else{
            $res = UserModel::singleton()->update();
            if(!$res){
                $this->error(User::singleton()->getError());
            }else{
                $this->success($res['id']?'更新成功！':'新增成功！', U('index'));
            }
        }

    }

    /**
     * 轮播图删除
     * @author xu
     */
    public function deltet(){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);

        $this->crmDelete ('CrmAds', $map );
    }


    /**
     * 导出
     * @author xu
     */
    public function export(){


        $type = I('type');

        $ids = I('ids');
        $name       =   I('name');
        $phone      =   I('phone');
        $hospital   =   I('hospital');
        $where['is_show']  =   array('eq',1);
        if ($type === 'CHECKED') {
            $where = ['id' => ['in', explode(',',$ids)]];
        } else if ($type === 'FILTERED' ) {
            $where['name']    =   array('like', '%'.(string)$name.'%');
            $where['phone']    =   array('like', '%'.(string)$phone.'%');
            $where['hospital']    =   array('like', '%'.(string)$hospital.'%');
        }
        //导出list
        $list = UserModel::singleton()->where($where)->order('id DESC')->select();
        //收藏数
        $collcet_rows = M()->table('tb_md_medical_record_collect')->where(['is_show'=>1])->field('user_id,count(0) count')->group('user_id')->select();
        $collcet_ids=[];
        foreach($collcet_rows as $key=>$val){
            $collcet_ids[$val['user_id']]=$val['count'];
        }
        //上传数
        $upload_rows = M()->table('tb_md_medical_record')->where(['is_show'=>1])->field('user_id,count(0) count')->group('user_id')->select();
        $upload_ids=[];
        foreach($upload_rows as $key=>$val){
            $upload_ids[$val['user_id']]=$val['count'];
        }


        foreach($list as $key=>$val){
            $list[$key]['index']=$key+1;
            $list[$key]['upload_count']=$upload_ids[$val['id']]?:0;
            $list[$key]['collect_count']=$collcet_ids[$val['id']]?:0;
        }

        $excelList = $list;
        //导出excel
        $fieldlist = array(
            "序号","姓名","医院","电话","职务","上传病例数","收藏病例数"
        );

        $field_list= array(
            "index","name","hospital","phone","title","upload_count","collect_count"
        );

        $caselist = array();
        for($i='A';$i<='G';$i++){
            $caselist[]=$i;
        }

        //单元格宽度
        $sheetWidth = array(
            "A"=>20,"B"=>20,"C"=>20,"D"=>30,"E"=>20,"F"=>20,"G"=>20
        );

        ini_set('memory_limit', '1024M');

        require_once __DIR__ . '/../../../ThinkPHP/Library/Vendor/PHPExcel-1.8.1/Classes/PHPExcel.php';
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle('医生列表');
        for($i=0;$i<count($fieldlist);$i++){
            $objPHPExcel->getActiveSheet()->setCellValue($caselist[$i] . 1, $fieldlist[$i]);
            //设置单元格居中（影响速度不设了）
            $objPHPExcel->getActiveSheet()->getStyle($caselist[$i] . 1)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

            //设置单元格宽度
            $objPHPExcel->getActiveSheet()->getColumnDimension($caselist[$i])->setWidth($sheetWidth[$caselist[$i]]);
        }


        for ($i = 2; $i <= count($excelList)+1; $i++) {
            $templist = array();
            foreach($field_list as $field){
                $templist[]=$excelList[$i-2][$field];
            }
            for($j=0;$j<count($templist);$j++){
                $objPHPExcel->getActiveSheet()->setCellValue($caselist[$j] . $i, $templist[$j]);
                //设置单元格居中（影响速度不设了）
                $objPHPExcel->getActiveSheet()->getStyle($caselist[$j] . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            }
        }
        header('Content-Disposition: attachment; filename=user.xlsx');
        \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007')->save("php://output");
    }

 
}